from django.shortcuts import render

# Create your views here.

from rest_framework.viewsets import ViewSet

from . import serializer

from luff.uitls.response import APIResponse

from rest_framework.decorators import action

import re

from .models import User

class LoginView(ViewSet):
    @action(methods=['POST'],detail=False)
    def login(self,request,*args,**kwargs):
        ser = serializer.UserSerializer(data=request.data)
        print(ser)
        if ser.is_valid():
            token = ser.context['token']

            username = ser.context['username']

            return APIResponse(token=token,username=username)
        return APIResponse(code=110, msg=ser.errors)

    @action(methods=['POST'],detail=False)
    def check_mobile(self,request,*args,**kwargs):
        mobile=request.GET.get('mobile')
        if re.match('1[3-9][0-9]{9}$',mobile):
            user=User.objects.filter(mobile=mobile).first()

            if user:
                return APIResponse(msg="手机号存在")
            else:
                return APIResponse(code=101,msg='手机号不存在')
        else:
            return APIResponse(code=102,mag='手机号不合法')


from luff.lib.tx_sms.tx_sms import send,get_code